package com.ygxsk.carhome.ui.consign;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.ClickUtils;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.gyf.immersionbar.ImmersionBar;
import com.ygxsk.carhome.R;
import com.ygxsk.carhome.enumwrap.ConsignMainTitle;
import com.ygxsk.carhome.response.CustomerTitle;
import com.ygxsk.carhome.ui.ErrorActivity;

import java.util.ArrayList;

import butterknife.BindView;

/*
 * @Author:itchenqi175@163.com
 * @Time:2023-08-24  09:53
 * @Description:寄售管理的 主页列表 添加切换
 */
public class ConsignMainTabActivity extends ErrorActivity {

    //装载的fragment
    private ArrayList<Fragment> fragmentLists = new ArrayList<>();

    //装载标题内容
    private ArrayList<CustomerTitle> titleLists = new ArrayList<>();

    //该界面和标题的绑定
    private TabLayoutMediator tabLayoutMediator;

    @BindView(R.id.tab_layout_consigncustomer)
    TabLayout tabLayoutInConsign;

    @BindView(R.id.viewpagerinstationtimeconsign)
    ViewPager2 viewPagerInStationConsign;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mActivityInstance = this;

        initViews();


    }


    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();

        ImmersionBar.with(this)//
                .titleBar(R.id.toolbarygxskout)
                .fitsSystemWindows(true)//解决状态栏和布局重叠问题，任选其一，默认为false，当为true时一定要指定statusBarColor()，不然状态栏为透明色，还有一些重载方法
                //.statusBarColor(R.color.colorPrimary)//
                //.navigationBarColor(R.color.colorPrimary)//
                //.keyboardEnable(true)
                .init();

    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_consign_main_tab;
    }

    @Override
    protected void initToolBar(ToolbarHelper toolbarHelper) {

        //这个可能是动态的，不同身份 是不同标题：服务站经理存款业绩排名
        toolbarHelper.setTitle(getString(R.string.title_car_consign));
        toolbarHelper.setVersionName(true, AppUtils.getAppVersionName());
        toolbarHelper.setLayoutClose(false, "", null);
        toolbarHelper.setLayoutLeft(true, R.drawable.backicon, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                methodBack();
            }
        });

//        toolbarHelper.setLayoutRight(false, 0, "", null);
        toolbarHelper.setLayoutRight(true, 1, "历史记录", new ClickUtils.OnDebouncingClickListener() {
            @Override
            public void onDebouncingClick(View v) {

                //历史记录的页面布局
                Bundle extra_bundle = new Bundle();
                ActivityUtils.startActivity(extra_bundle, ConsignHistoryTabActivity.class);

            }
        });

    }

    @Override
    protected void onClickNodata(View v) {

        //retain

    }

    @Override
    protected void initViews() {

        //初始化fragments
        if (fragmentLists == null) {
            fragmentLists = new ArrayList<>();
        }

        if (titleLists == null) {
            titleLists = new ArrayList<>();
        }

//        public enum ConsignMainTitle {
//            TITLE_ALL("100", "全部"),
//            TITLE_NOT_CONTACT("0", "待联系"),
//            TITLE_HAS_CONTACT("1", "已联系"),
//            TITLE_HAS_VERIFY("2", "已审核");

        CustomerTitle customerTitle_assigned = new CustomerTitle();
        customerTitle_assigned.setTitleName(ConsignMainTitle.TITLE_ALL.getValue());//全部

        CustomerTitle customerTitle_confirmed = new CustomerTitle();
        customerTitle_confirmed.setTitleName(ConsignMainTitle.TITLE_NOT_CONTACT.getValue());//

        CustomerTitle customerTitle_fight_fail = new CustomerTitle();
        customerTitle_fight_fail.setTitleName(ConsignMainTitle.TITLE_HAS_CONTACT.getValue());//

        CustomerTitle customerTitle_fight_success = new CustomerTitle();
        customerTitle_fight_success.setTitleName(ConsignMainTitle.TITLE_HAS_VERIFY.getValue());//

        titleLists.add(customerTitle_assigned);
        titleLists.add(customerTitle_confirmed);
        titleLists.add(customerTitle_fight_fail);
        titleLists.add(customerTitle_fight_success);

        fragmentLists.add(ConsignMainFragment.newInstance(ConsignMainTitle.TITLE_ALL.getIndex(), ConsignMainTitle.TITLE_ALL.getValue()));
        fragmentLists.add(ConsignMainFragment.newInstance(ConsignMainTitle.TITLE_NOT_CONTACT.getIndex(), ConsignMainTitle.TITLE_NOT_CONTACT.getValue()));
        fragmentLists.add(ConsignMainFragment.newInstance(ConsignMainTitle.TITLE_HAS_CONTACT.getIndex(), ConsignMainTitle.TITLE_HAS_CONTACT.getValue()));
        fragmentLists.add(ConsignMainFragment.newInstance(ConsignMainTitle.TITLE_HAS_VERIFY.getIndex(), ConsignMainTitle.TITLE_HAS_VERIFY.getValue()));

        MyConsignCustomerAdapter myTimeCustomerAdapter = new MyConsignCustomerAdapter(this, fragmentLists);
        viewPagerInStationConsign.setAdapter(myTimeCustomerAdapter);

        //设置字体颜色，第一个参数代表未点击时的字体颜色，第二个参数代表点击后的颜色
        //tabLayoutInCustomer.setTabTextColors(R.color.black,R.color.teal_700);
        //点击单个tab时背景为透明色
        //tabLayout.setTabRippleColor(ColorStateList.valueOf(Color.TRANSPARENT));

        //这种是不是将来还需要接口回来，重新绑定
        //让viewpager2与tablayout关联，实现滑动效果（滑动fragment底部tab变化，tab滑动fragment跟着滑动）
        tabLayoutMediator = new TabLayoutMediator(tabLayoutInConsign, viewPagerInStationConsign, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                //标题和数量的合并
                tab.setText(titleLists.get(position).getTitleName());
            }
        });

        tabLayoutMediator.attach();//一定要attach（）

    }

    @Override
    protected void gainDatas() {

        //retain

    }

    @Override
    protected void methodBack() {
        if (ActivityUtils.isActivityAlive(this)) {
            ActivityUtils.finishActivity(this);
        }
    }

    /*
     * @Author:itchenqi175@163.com
     * @Time:2023-03-08  08:31
     * @Description:到期客户 左右两页的选项卡 适配器;
     */
    private class MyConsignCustomerAdapter extends FragmentStateAdapter {
        private ArrayList<Fragment> fragmentList;

        public MyConsignCustomerAdapter(@NonNull FragmentActivity fragmentActivity, ArrayList<Fragment> fragments) {
            super(fragmentActivity);
            this.fragmentList = fragments;
        }

        @NonNull
        @Override
        public Fragment createFragment(int position) {
            return fragmentList.get(position);
        }

        @Override
        public int getItemCount() {
            return fragmentList.size();
        }

    }
}